package com.cdu.guoxinan.www.smartcom.controller.propertyController;

import com.cdu.guoxinan.www.smartcom.aop.Annotation.OperationLog;
import com.cdu.guoxinan.www.smartcom.domian.dto.FeeDTO;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Fee;
import com.cdu.guoxinan.www.smartcom.domian.vo.FeeVO;
import com.cdu.guoxinan.www.smartcom.service.FeeService;
import com.cdu.guoxinan.www.smartcom.unit.model.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/fee")
@Api(tags = "物业费用管理")
public class FeeController {

    @Resource
    private FeeService feeService;


    @GetMapping("/all")
    @ApiModelProperty("获取所有物业费用")
    @OperationLog(description = "获取所有物业费用")
    R getAllFees(FeeDTO feeDTO){
        PageInfo<FeeVO> fees = feeService.getAllFees(feeDTO);

        return R.ok(fees, fees.getTotal());
    }


    @GetMapping("/types")
    @ApiModelProperty("获取所有物业费用类型")
    R getFeeTypes(){
        return R.ok(feeService.getFeeTypes());
    }
    @GetMapping("/status")
    @ApiModelProperty("获取所有物业费用状态")
    @OperationLog(description = "获取所有物业费用状态")
    R getFeeStatus(){
        return R.ok(feeService.getFeeStatus());
    }

    @PostMapping("/add")
    @ApiModelProperty("添加物业费用")
    @OperationLog(description = "添加物业费用")
    R addResidentFee(@RequestBody FeeVO  feeVO){
        int i = feeService.addResidentFee(feeVO);
        if (i==-1){
            return R.error("没有该居民");
        }

        return  R.ok("添加成功");
    }

    @PostMapping("/delete")
    @ApiModelProperty("删除物业费用")
    @OperationLog(description = "删除物业费用")
    R deleteResidentFee(Integer feeId){
        return feeService.deleteResidentFee(feeId)==1?R.ok("删除成功"):R.error("删除失败");
    }
    @PostMapping("/update")
    @ApiModelProperty("修改物业费用")
    @OperationLog(description = "修改物业费用")
    R updateResidentFee(@RequestBody Fee fee){
        System.out.println("fee:"+fee);
        return feeService.updateResidentFee(fee)==1?R.ok("修改成功"):R.error("修改失败");
    }
    @PostMapping("/deleteBatch")
    @ApiModelProperty("批量删除物业费用")
    @OperationLog(description = "批量删除物业费用")
    R deleteResidentFees(@RequestBody Integer[] feeIds){


        return feeService.deleteResidentFees(feeIds)==feeIds.length?R.ok("删除成功"):R.error("删除失败");
    }

    @GetMapping("/getById")
    @ApiModelProperty("通过ID获取物业费用")
    @OperationLog(description = "通过ID获取物业费用")
    R getById(Integer  residentId){
        List<FeeVO> fee = feeService.getById(residentId);
        return fee!=null?R.ok(fee):R.error("没有该物业费用");
    }
}
